package com.etrans.web.gps.business.system.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.system.domain.RoleVO;

@Service
public class RoleService extends BaseService {

	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,Exception.class })
	public int saveRole(RoleVO role) {
		return this.insert("roleMapper.saveRole", role);
	}
	
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,Exception.class })
	public void saveRoleMenu(RoleVO role) {
		int roleId = role.getId();
		this.deleteRoleMenu(roleId);
		
		String menuIds = role.getMenuIds();
		if(menuIds != null && !"".equals(menuIds)){
			String[] menuIdArray = menuIds.split(",");
			for (String menuId : menuIdArray) {
				HashMap<String,Object> params = new HashMap<String, Object>();
				params.put("roleId", roleId);
				params.put("menuId", menuId);
				this.insert("roleMapper.saveRoleMenu", params);
			}
		}
	}

	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,Exception.class })
	public int updateRole(RoleVO role) {
		return this.update("roleMapper.updateRole", role);
	}

	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,Exception.class })
	public void deleteRole(int roleId) {
		this.delete("roleMapper.deleteRole", roleId);
	}
	
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,Exception.class })
	public void deleteRoleMenu(int roleId) {
		this.delete("roleMapper.deleteRoleMenu", roleId);
	}

	public RoleVO queryRoleById(String id) {
		return (RoleVO) this.selectOne("roleQuery.queryRoleById", id);
	}

	public List<RoleVO> queryAllRole(HashMap<String,Object> param) {
		return this.selectList("roleQuery.queryAllRole", param);
	}
	
	public Long allRoleCount(HashMap<String,Object> param) {
		return (Long)this.selectOne("roleQuery.allRoleCount", param);
	}
	
	public RoleVO queryRoleByRoleId(Integer roleId) {
		return (RoleVO) this.selectOne("roleQuery.queryRoleByRoleId", roleId);
	}
	
	public String queryRoleMenu(Integer roleId) {
		StringBuffer sb = new StringBuffer();
		List<HashMap<String,Object>> list = this.selectList("roleQuery.queryRoleMenu", roleId);
		if(list != null && list.size() > 0){
			for (HashMap<String, Object> map : list) {
				sb.append(map.get("menu_id")).append(",");
			}
			return sb.deleteCharAt(sb.lastIndexOf(",")).toString();
		}else{
			return "";
		}
	}
	
	/**
	 * 判断是否存在相同的角色名
	 * @param param
	 * @return
	 */
	public Integer roleNameCount(RoleVO role) {
		return  (Integer)this.selectOne("roleQuery.roleNameCount", role);
	}

	/**
	 * 所属角色下拉框
	 * @return
	 */
	public List<HashMap<String,Object>> queryRolesSelect() {
		return this.selectList("roleQuery.queryRolesSelect", null);
	}
}
